(function($, doc) {
	$.init();
	$.plusReady(function(){
		var user = app.getState();
		var listdata = localStorage.getItem('coinlist');
		var coinlist = JSON.parse(listdata);
		var chkhas = function(addr){
			if(coinlist[addr]){
				return true;
			}
			return false;
		}
		doc.getElementById('step1btn').addEventListener('tap',function(){
			var addr = doc.getElementById('address').value;
			if(!addr || addr.length < 32) {
				$.toast("合约地址错误");
				return;
			}
			if(chkhas(addr)){
				$.toast("系统中存在该地址！");
				return;
			}
			var postdata = {address : addr};
			$.post(coinurl + 'eth/add', postdata, function(res){
				if(res && res.status < 1) {
					$.toast(res.msg);
					return;
				}else{
					doc.getElementById('step1').style.display = 'none';
					doc.getElementById('step2').style.display = '';
					doc.getElementById('addr').value = res.address;
					doc.getElementById('name').value = res.name;
					doc.getElementById('abi').value = res.abi;
					doc.getElementById('logo').src = serverurl + res.logo;
				}
			});
			
		});
		doc.getElementById('step2btn').addEventListener('tap',function(){
			var addr = doc.getElementById('addr').value;
			if(!addr || addr.length < 32 || chkhas(addr)){
				$.toast("合约地址错误");
				return;
			}
			var name = doc.getElementById('name').value;
			var abi = doc.getElementById('abi').value;
			var logo = doc.getElementById('logo').src.replace(serverurl, '');
			coinlist[addr] = {
				address : addr,
				name : name,
				coinname : name,
				logo : logo,
				//abi : abi,
				ctype : 'ethcontact'
			};
			localStorage.setItem('coinlist', JSON.stringify(coinlist));
			localStorage.setItem(addr + 'abi',abi);
			$.toast("添加成功！");
			plus.webview.close('addcoin', "none");
			plus.webview.close('coinlist', "none");
			$.openWindow({'url' : 'coinlist.html', id : 'coinlist'});
		});
	});
}(mui, document));